using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using ZephyrisNet_WP7;

namespace Zephyris_Space_Mercs
{
    public class BulletObject
    {
        public enum BulletType { plasma, missile };

        public BulletType bulletType;
        public float damage;
        public Model bulletModel;

        public bool terminate = false;
        public Guid ownerId;
        public double created;

        public Vector3 pos;
        public float velocity;
        public Quaternion rot;

        private Quaternion internalRot = Quaternion.Identity;
        private Matrix worldMatrix = Matrix.Identity;

        private CollisionDetector collisionDetector = new CollisionDetector();

        public BoundingSphere boundingSphere = new BoundingSphere();

        public void Init(Guid ownerId, Model bulletModel, Vector3 pos, Quaternion rotation, float velocity, BulletType bulletType, float damage)
        {
            this.created = Helpers.Epoch();
            this.ownerId = ownerId;

            this.bulletModel = bulletModel;
            this.pos = pos;
            this.rot = rotation;
            this.velocity = velocity;

            this.bulletType = bulletType;
            this.damage = damage;

            this.boundingSphere = collisionDetector.GetBoundingSphere(this.pos, this.bulletModel, 0.2f);
        }

        public void Update()
        {
            if (Helpers.Epoch() - created > 1000)
            {
                terminate = true;
            }

            if (!terminate)
            {
                Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), rot);
                pos += addVector * velocity;

                boundingSphere = collisionDetector.UpdateBoundingSphere(pos, boundingSphere.Radius);
            }
        }

        public void Draw(float scale, float alpha)
        {
            if (!terminate)
            {
                worldMatrix = Matrix.CreateScale(scale, scale, scale) * Matrix.CreateTranslation(pos);
                //worldMatrix = Matrix.CreateScale(0.001f, 0.001f, 0.001f) * Matrix.CreateRotationY(MathHelper.Pi) * Matrix.CreateTranslation(player.pos);
                //worldMatrix = Matrix.CreateScale(0.0001f, 0.0001f, 0.0001f) * Matrix.CreateTranslation(player.pos);

                // Copy any parent transforms.
                Matrix[] transforms = new Matrix[bulletModel.Bones.Count];
                bulletModel.CopyAbsoluteBoneTransformsTo(transforms);

                // Draw the model. A model can have multiple meshes, so loop.
                foreach (ModelMesh mesh in bulletModel.Meshes)
                {
                    // This is where the mesh orientation is set, as well
                    // as our camera and projection.
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.View = Camera.view;
                        effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                        effect.Projection = Camera.projection;
                        effect.Alpha = alpha;
                        effect.EmissiveColor = new Vector3(0.1f, 0.1f, 1.0f);
                    }
                    // Draw the mesh, using the effects set above.
                    mesh.Draw();
                }
            }

        }
    }
}
